package org.hepeng.commons.spring.cloud.config;

import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.cloud.config.server.environment.JGitEnvironmentRepository;
import org.springframework.cloud.config.server.environment.SearchPathCompositeEnvironmentRepository;

import java.util.Map;
import java.util.Objects;

/**
 * @author he peng
 */
public abstract class AbstractAlterConfigRepositoryParamHandler implements AlterConfigRepositoryRequestParamHandler {

    @Override
    public EnvironmentRepository handle(EnvironmentRepository repository, Map<String, Object> requestParam) {
        EnvironmentRepository returnRepository = repository;
        if (SearchPathCompositeEnvironmentRepository.class.isAssignableFrom(repository.getClass())) {
            JGitEnvironmentRepository gitRepository = JGitRepositoryUtils.getFirstRepository((SearchPathCompositeEnvironmentRepository) repository);
            if (Objects.nonNull(gitRepository)) {
                returnRepository = handleGitRepository(gitRepository , requestParam);
            }
        }
        return returnRepository;
    }

    protected abstract EnvironmentRepository handleGitRepository(JGitEnvironmentRepository repository, Map<String, Object> requestParam);

}
